package com.talkweb.newproject.admin.controller;

import com.talkweb.newproject.admin.rest.SysUserRest;
import com.talkweb.newproject.admin.sysuser.dto.SysUserDTO;
import com.talkweb.newproject.api.model.admin.SysUserParam;
import com.talkweb.newproject.api.model.common.ResponseResult;
import com.talkweb.newproject.api.model.common.Status;
import com.talkweb.newproject.common.utils.EncryptUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by qsf on 2019/4/17.
 */
@RestController
@RequestMapping(value="sysuser-api")
@Slf4j
public class SysUserController {
    @Autowired
    private SysUserRest sysUserRest;

    @RequestMapping(value="/addUser",method = RequestMethod.POST)
    public ResponseResult<Object> addUser(@RequestBody SysUserDTO vo) throws Exception {
        ResponseResult<Object> result = new ResponseResult<>();
        vo.setPassword(EncryptUtil.encryptPsw(vo.getPassword()));
        vo.setValidateState("1");
        try{
            if(vo!=null && vo.getId()>0) {
                if("0".equals(vo.getIsLocked())) {
                    vo.setPassword("");
                }
                result=sysUserRest.update(vo);
            }else {
                result= sysUserRest.create(vo);
            }
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }

    @RequestMapping(value="/querySysUser",method = RequestMethod.POST)
    public ResponseResult<SysUserDTO> querySysUser(@RequestParam("id") String id) throws Exception {
        ResponseResult<SysUserDTO> result = new ResponseResult<>();
        try{
            if(id!= null &&id.length()!=0) {
                result=sysUserRest.querySysUserByPrimaryKey(id);
            }else {
                result.setStatus(Status.FAILD);
                result.setMessage("执行异常,参数不完整");
            }
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 查询用户列表
     * @param vo
     * @return
     * @throws Exception
     */
    @RequestMapping(value="querySysUserList",method = RequestMethod.POST)
    public Object querySysUserList(@RequestBody SysUserParam vo) throws Exception {
        Object result = new ResponseResult<>();
        try{

            return sysUserRest.querySysUserList(vo);
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            return result;
        }
    }

    /**
     * 删除用户信息
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/delSysUserByUserId",method = RequestMethod.POST)
    public ResponseResult<Object> delSysUserById(@RequestParam("ids") String ids) throws Exception {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            if(ids!= null &&!"".equals(ids)) {
                sysUserRest.delSysUserByUserId(ids);
                result.setStatus(Status.SUCCESS);
                result.setMessage("删除成功");
            }else {
                result.setStatus(Status.FAILD);
                result.setMessage("删除失败,请重试");
            }
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }

    @RequestMapping(value="/checkOldPassWord",method = RequestMethod.POST)
    public ResponseResult<Object> checkOldPassword(@RequestParam("LoginName") String loginName,@RequestParam("oldPassword") String oldPassword,@RequestParam("newPassword") String newPassword) throws Exception {
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            if(loginName != null && !"".equals(loginName)) {
                result = sysUserRest.checkOldPassWord(loginName,oldPassword,newPassword);
            }else {
                result.setStatus(Status.FAILD);
                result.setMessage("用户名不能为空");
            }
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setMessage("执行异常,请重试");
            result.setStatus(Status.FAILD);
            return result;
        }

    }

}
